<script language="javascript" src="components/com_sale_tsc_thu_ngan/assets/jquery.min.js"></script>
<script language="javascript" src="components/com_sale_tsc_thu_ngan/assets/jquery-ui.min.js"></script>
<script language="javascript" src="components/com_sale_tsc_thu_ngan/assets/jquery-1.8.2.js"></script>
<style>
.suggestionsBox {
		/*position: absolute;*/
		left: 30px;
		margin: 20px 0px 0px 0px;
		width: 200px;
		background-color: #212427;
		-moz-border-radius: 7px;
		-webkit-border-radius: 7px;
		border-radius: 4px;
		border: 2px solid #000;	
		color: #fff;
	}
	
	.suggestionList {
		margin: 0px;
		padding: 0px;
	}
	
	.suggestionList li {
		
		margin: 0px 0px 3px 0px;
		padding: 3px;
		cursor: pointer;
	}
	
	.suggestionList li:hover {
		background-color: #659CD8;}
</style>
<script type="text/javascript" src="components/com_sale_tsc_thu_ngan/assets/jquery.js"></script>

<script type="text/javascript">
$(function(){
$("#txtkhachdua").keyup(function () {
var value = $(this).val();
$("#Label1").html(numberFormat(value,0,'',','));
}).blur(function () {
var value = $(this).val();
if(value == "" || value == 0)
{
$("#txtkhachdua").val('0').keyup();
}
});
});
function numberFormat(number, decimals, dec_point, thousands_sep) {
var n = number, prec = decimals;
n = !isFinite(+n) ? 0 : +n;
prec = !isFinite(+prec) ? 0 : Math.abs(prec);
var sep = (typeof thousands_sep == "undefined") ? '.' : thousands_sep;
var dec = (typeof dec_point == "undefined") ? ',' : dec_point;
var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
var abs = Math.abs(n).toFixed(prec);
var _, i;
if (abs >= 1000) {
_ = abs.split(/\D/);
i = _[0].length % 3 || 3;
_[0] = s.slice(0, i + (n < 0)) +
_[0].slice(i).replace(/(\d{3})/g, sep + '$1');
s = _.join(dec);
} else {
s = s.replace(',', dec);
}
return s;
}
</script>

<script type="text/javascript">
$(function(){
$("#txtthoilai").keyup(function () {
var value = $(this).val();
$("#Label2").html(numberFormat(value,0,'',','));
}).blur(function () {
var value = $(this).val();
if(value == "" || value == 0)
{
$("#txtthoilai").val('0').keyup();
}
});
});
function numberFormat(number, decimals, dec_point, thousands_sep) {
var n = number, prec = decimals;
n = !isFinite(+n) ? 0 : +n;
prec = !isFinite(+prec) ? 0 : Math.abs(prec);
var sep = (typeof thousands_sep == "undefined") ? '.' : thousands_sep;
var dec = (typeof dec_point == "undefined") ? ',' : dec_point;
var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
var abs = Math.abs(n).toFixed(prec);
var _, i;
if (abs >= 1000) {
_ = abs.split(/\D/);
i = _[0].length % 3 || 3;
_[0] = s.slice(0, i + (n < 0)) +
_[0].slice(i).replace(/(\d{3})/g, sep + '$1');
s = _.join(dec);
} else {
s = s.replace(',', dec);
}
return s;
}
</script>
<script type="text/javascript">
	function lookup(txtmkh) { //alert(txtmkh);
		if(txtmkh.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("components/com_sale_tsc_thu_ngan/views/thanh_toan/lay_mkh.php", {txtmkh: ""+txtmkh+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue) {
		$('#txtmkh').val(thisValue);
        //var id_cus = $("#txtmkh").val();
		setTimeout("$('#suggestions').hide();", 200);
	}
</script>
<script>
var id_pro = new Array();
var id_type = new Array();
var dg  = new Array();
var sl = new Array();
var tt = new Array();
var coun = 0;

$(document).ready(function(){
    
    //kiem tra tien khach tra
    $("#txtkhachdua").change(function(){
       //alert($("#tk_the").val());
        var tong =  eval($("#sum").text());
        var tk = eval($("#txtkhachdua").val());
        if(tk<tong)
        {
            alert("Khách đưa không đủ");
            $("#txtkhachdua").val('');
        }
        else
        {
            
            var tl = tk - tong;
            //alert(tl);
            $("#txtthoilai").val(tl);
        }
        //txtkhachdua txtthoilai
    });
    
//kiem tra coppon
    $("#txtcup").change(function(){
        var cp = $("#txtcup").val();
        var su = $("#sum").text(); 
        su = eval(su);
        //alert(su);
        //xu ly ajax
        $.ajax({
			type:"get",
			url:"components/com_sale_tsc_thu_ngan/views/thanh_toan/kiemtra.php",
            //url:"index.php/?option=com_kinh_doanh",
            data:"cp="+cp,
			async:false,
			success:function(string){ 
			    if(string != "0")
                {
                    
                    var ar = $.parseJSON(string);
                     
                     $("#idcup").val(ar.id_cup);
                    if(ar.flag==1)
                    {
                            var cp = ar.money;
                            
                            $("#coppon").text("Giá trị cuppon: "+ar.money);
                            cp = eval(cp);
                            for(i=0;i<coun;i++)
                            {
                                if(id_pro[i] == ar.id_product)
                                    {
                                        
                                        var ar = eval(tt[i]) - cp;
                                        
                                        if(ar < 0)
                                        {
                                            ar=0;
                                        }
                                        tt[i] = ar;
                                        
                                        tt_sau_cp = 0;         
                                        for(x=0;x<coun;x++)
                                        {  
                                            tt_sau_cp+=eval(tt[x]);
                                        }
                                        $("#sum").text(tt_sau_cp);
                                        $("#txtsum").val(tt_sau_cp);
                                        
                                    } 
                            }
                            
                    }
                    
                    //cuppon tru phan tram vao 1 mon hang
                    if(ar.flag==2)
                    {
                            var cp = ar.persent;
                            $("#coppon").text("Giá trị cuppon: "+ar.persent+"%");
                            cp = eval(cp);
                            for(i=0;i<coun;i++)
                            {
                                if(id_pro[i] == ar.id_product)
                                    {
                                        
                                        var ar = eval(tt[i]) * cp / 100;
                                        //alert(ar);
                                        tt[i] = ar;
                                        tt_sau_cp = 0;         
                                        for(x=0;x<coun;x++)
                                        {
                                            tt_sau_cp+=eval(tt[x]);
                                        }
                                        $("#sum").text(tt_sau_cp);
                                        $("#txtsum").val(tt_sau_cp);
                                        //alert($("#txtsum").val());
                                    } 
                            }
                            
                    }
                    
                    //cuppon tru tien vao 1 loai hang
                    if(ar.flag==3)
                    {
                            var cp = ar.money;
                            $("#coppon").text("Giá trị cuppon: "+ar.money);
                            cp = eval(cp);
                            var so_tien_can_tru = 0;                            
                            var tong_tien = 0;
                            for(i=0;i<coun;i++)
                            {
                                
                                //tinh tong cua loai can tru
                                if(id_type[i] == ar.id_product_type)
                                    {
                                        so_tien_can_tru += eval(tt[i]);
                                    } 
                                //tong cua nhung loai khac
                                else
                                {
                                    tong_tien += eval(tt[i]);
                                }
                                    
                            }

                            if(so_tien_can_tru - cp > 0)
                            {
                                tong_tien = tong_tien + (so_tien_can_tru  - cp);
                            }
                            //tong_tien = tong_tien + (so_tien_can_tru - cp);
                             $("#sum").text(tong_tien);
                                $("#txtsum").val(tong_tien);
                    }
                    
                    //cuppon tru phan tram vao 1 loai hang
                    if(ar.flag==4)
                    {
                            var cp = ar.persent;
                            $("#coppon").text("Giá trị cuppon: "+ar.persent+"%");
                            cp = eval(cp);
                            var so_tien_can_tru = 0;                            
                            var tong_tien = 0;
                            for(i=0;i<coun;i++)
                            {
                                //tinh tong cua loai can tru
                                if(id_type[i] == ar.id_product_type)
                                    {
                                        so_tien_can_tru += eval(tt[i]);
                                    } 
                                //tong cua nhung loai khac
                                else
                                {
                                    tong_tien += eval(tt[i]);
                                }
                                    
                            }
                            
                            tong_tien = tong_tien + (so_tien_can_tru * cp / 100 );
                            
                            //tong_tien = tong_tien + (so_tien_can_tru - cp);
                             $("#sum").text(tong_tien);
                             $("#txtsum").val(tong_tien);
                    }
                }
                else
                {
                    alert("Sai mã cuppon.");
                    $("#txtcup").val('');
                    var r = $("#tc").text();
                    $("#sum").text(r);
                    $("#coppon").hide();                   
                }
                
			}
		});
    });
    
    //lay balance
    $("#btncheck").click(function(){
       var id = $("#txtmkh").val();
       var su = eval($("#sum").text());
       //alert(id); 
       $.ajax({
			type:"post",
			url:"components/com_sale_tsc_thu_ngan/views/thanh_toan/balance.php",
            //url:"index.php/?option=com_kinh_doanh",
            data:"cp="+id,
			async:false,
			success:function(string){ 
			     $("#tien_trong_tk").text(string);
			     if(eval($("#tien_trong_tk").text() < su))
                 {
                    alert("Số dư trong tài khoản: "+string+" nhỏ hơn số tiền cần trả.");
                    $("#check_tk").hide();
                    $("#check_tm").show();
                    $("#txtthoilai").val("");
                    $("#txtkhachdua").val("");
                    $("#tien_mat").click();
                 }
			}
            });
    });
    
    
    //check ratio
    $("#tk_the").click(function(){
       //alert($("#tk_the").val());
        $("#check_tk").show();
        $("#check_tm").hide();
        //$("#txtmkh").val("");
         $("#tien_trong_tk").text("");
    });
    $("#tien_mat").click(function(){
       $("#check_tk").hide();
       $("#check_tm").show();
       $("#txtthoilai").val("");
       $("#txtkhachdua").val("");
    });
    
    
    });
</script>

<?php 
	jimport('joomla.application.component.view');
	//thuc hien goi  model phu hop
	class sale_tsc_thu_nganViewthanh_toan extends JView
	{
		function display($tpl=null)
		{
            $model = $this->getModel();
            $post = JRequest::get('post',4);   	
            $data = $model->sp_bill($post['id_bill']);	  
            //print_r($data);
            ?><form action="index.php?option=com_sale_tsc_thu_ngan&task=hoan_tat" method="post">
            <input type="hidden" value="<?php echo $post['id_bill'];?>" name="id_bill" />
            <input type="hidden" value="<?php echo $data[0]->id_cu;?>" name="id_cu" />
            <input type="hidden" value="<?php echo $data[0]->id_buz;?>" name="id_buz" />
            <input type="hidden" id="idcup" name="idcup" />
                <table border ="1">
                <tr>
                    <td>
                        Số thứ tự
                    </td>
                    <td>
                        Tên sản phẩm
                    </td>
                    <td>
                        Đơn giá
                    </td>
                    <td>
                        Số lượng
                    </td>
                    <td>
                        Thành tiền
                    </td>
                </tr>
                <?php $i= 1;
                $sum = 0;
                    foreach($data as $row)
                    {
                        ?>
                        <script>
                        //alert(count);
                        id_pro[coun] = "<?php echo $row->id_product; //id san pham?>";
                        id_type[coun] = "<?php echo $row->id_type; //id loai san pham?>";
                        dg[coun] = "<?php echo $row->price; //don gia?>";
                        sl[coun] = "<?php echo $row->number; //so luong?>";
                        
                    </script>
                        <tr>
                            <td>
                                <?php echo $i;?>
                            </td>
                            <td>
                                <?php echo $row->pro_name;?>
                            </td>
                            <td>
                                <?php echo $row->price;?>
                            </td>
                            <td>
                                <?php echo $row->number;?>
                            </td>
                            <td>
                                <?php 
                                    if($row->status == 3){
                                echo $row->price * $row->number;
                                $sum +=$row->price * $row->number;
                                ?>
                                <script>
                                tt[coun] = "<?php echo $row->price * $row->number; //so luong?>";
                                coun++;
                                </script><?php } ?>
                            </td>

                            </tr>
                        <?php
                    }
                ?>
            </table>
            Tổng cộng: <span id="tc"><?php echo $sum;?></span><?php //echo "(".$row->symbol.")"?>
            <br />
            Coupon: <input type="text" name="txtcup" id="txtcup" />
            <br /><span id="coppon"></span><br />
            Sau khi Trừ cuppon: <span id="sum"><?php echo $sum;?></span>
            <input type="hidden" name="txtsum" id="txtsum" value="<?php echo $sum;?>"/>
            <br />mã khách hàng:<input type="text" value="" id="txtmkh" onkeyup="lookup(this.value);" onblur="fill();" name="txtmkh" />
            <div class="suggestionsBox" id="suggestions" style="display: none;" >
                                <div class="suggestionList" id="autoSuggestionsList">
                                
                                &nbsp;
                                
                                </div>
                                
                                </div><br />
            <input type="radio" name="thanhtoan" id="tk_the" value="tk_the"/>Tài Khoản Thẻ 
            <span id="check_tk" style="display: none;">
                                <input type="button" value="Check" id="btncheck" />
                                Tiền trong tài khoản: <span id="tien_trong_tk"></span><br /></span>
                                <br /><input type="radio" name="thanhtoan" id="tien_mat" value="tien_mat"/>Tiền Mặt<br /><span id="check_tm" style="display: none;">
            Khách Đưa: <input type="text" name="txtkhachdua" id="txtkhachdua" /><label id="Label1" style="color:#f00;"></label>
            <br />Thối lại<input type="text" name="txtthoilai" id="txtthoilai" /><label id="Label2" style="color:#f00;"></label>
            </span><br />
            <input type="submit" value="Hoàn tất" />
            </form>
            <?php
		}
    }
?>